package ch.blinkenlights.android.vanilla;

import android.content.Context;

import java.util.ArrayList;

/**
 * @author zollty
 * @since 2023-09-05
 */
public class PlaybackPlaylistService {
	private static final String PICK_PLAYLIST_NAME = "挑歌";
	private static final String TEMP_PLAYLIST_NAME = "临时";
	private static Long pickPlListId = null;
	private static Long tempPlListId = null;

	private final Context context;

	public PlaybackPlaylistService(Context context) {
		this.context = context;
	}

	protected void addToPickPlaylist(Song song) {
		if (song != null) {
			if(pickPlListId == null) {
				pickPlListId = getPickPlaylistId(context, true);
			}
			ArrayList<Long> audioIds = new ArrayList<Long>();
			audioIds.add(song.id);
			if (!Playlist.isInPlaylist(context, pickPlListId, song)) {
				Playlist.addToPlaylist(context, pickPlListId, audioIds);
			}

			if(tempPlListId == null || tempPlListId == -1) {
				tempPlListId = Playlist.getPlaylist(context, TEMP_PLAYLIST_NAME);
			}
			// 从临时歌单中移除
			if (tempPlListId != -1) {
				Playlist.removeFromPlaylist(context, tempPlListId, audioIds);
			}
		}
	}
	private long getPickPlaylistId(Context context, boolean create) {
		long playlistId = Playlist.getPlaylist(context, PICK_PLAYLIST_NAME);
		if (playlistId == -1 && create)
			playlistId = Playlist.createPlaylist(context, PICK_PLAYLIST_NAME);
		return playlistId;
	}
}
